<?php
$this->load->view(
		'admin/layout/head', array(
		'title'=>'后台管理',
		'nav' => 'product',
		'css'=>['/assets/select2/css/select2.min.css']
	),
		FALSE);
switch ($this->session->admin_language) {
	case 'chinese':
		$prefix = '';
		break;
	case 'english':
		$prefix = 'en_';
		break;
	default:
		$prefix = '';
		break;
}
?>

<style>
	.images img {
		margin: 10px;
	}
</style>

<div id="container">
	<form class="form-inline" id="search-form">
	  <div class="form-group">
	  	<label for=""><span class="glyphicon glyphicon-star"></span> 评分</label>
	    <select name="star" class="form-control">
	    	<option value="">全部</option>
	    	<?php foreach ([1,2,3,4,5] as $index): ?>
	    		<option value="<?= $index ?>" <?= $this->input->get('star') != $index ?: 'selected' ?>>
	    			<?= $index ?>
	    		</option>
	    	<?php endforeach ?>
	    </select>
	  </div>
	  <div class="form-group">
	  	<label for="">选择商品</label>
	  	<select name="product_id" class="brand-slt" class="form-control" style="width: 300px;">
	  		<option value=""><?= $this->lang->line('all') ?></option>
	  		<?php foreach($products as $product): ?>
	  		<option value="<?= $product['id']?>" <?= $this->input->get('product_id') != $product['id'] ?: 'selected' ?>><?= $product[$prefix.'title'] ?></option>
	  		<?php endforeach; ?>
	  	</select>
	  </div>
	  <button class="btn btn-info"><?= $this->lang->line('search') ?></button>
	</form>

	<table class="table table-bordered table-striped table-hover scroll-table mt-20">
		<thead>
			<tr>
				<th width="300" class="text-center"><?= $this->lang->line('product') ?></th>
				<th width="140" class="text-center"><?= $this->lang->line('item') ?></th>
				<th width="200" class="text-center"><?= $this->lang->line('star') ?></th>
				<th width="80" class="text-center"><?= $this->lang->line('status') ?></th>
				<th width="100" class="text-center"><?= $this->lang->line('actions') ?></th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ($pages->data() as $key => $value): ?>
				<tr>
					<td class="text-center"><?= $value['product'] ? $value['product'][$prefix.'title'] : '' ?></td>
					<td class="text-center"><?= $value['item'] ? $value['item'][$prefix.'title'] : '' ?></td>
					<td class="text-center"><?= $value['star'] ? str_repeat('<span class="glyphicon glyphicon-star"></span>', $value['star']) : '<span style="color: #ccc;" class="glyphicon glyphicon-star"></span>' ?></td>
					<td class="text-center">
						<?= $value['status'] ? '显示' : '不显示' ?>
					</td>
					<td class="text-center" data-id="<?= $value['id'] ?>">
						<a href="#" class="hidden-evaluate" data-value="<?= $value['status'] ?>"><?= $value['status'] ? $this->lang->line('hidden') : $this->lang->line('show'); ?></a>
						<a href="#" class="show-detail"><?= $this->lang->line('detail') ?></a>
						<a href="javascript:;" class="del-btn"><?= $this->lang->line('delete') ?></a>
					</td>
				</tr>
			<?php endforeach ?>
		</tbody>
		<?php if ($pages->last_page > 1): ?>
			<tfoot>
				<td colspan="4" style="text-align:right">
					<?= $pages->render() ?>
				</td>
			</tfoot>
		<?php endif ?>
	</table>
</div>

<div class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4>评价详情</h4>
      </div>
      <div class="modal-body">
        
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<?php 
$this->load->view('admin/layout/footer', ['js'=>['/assets/select2/js/select2.full.min.js']]);
?>

<script>
$(function () {
	$('.brand-slt').select2();

	$('.hidden-evaluate').on('click', function () {
		var id = $(this).parents('td').attr('data-id');
		var status = $(this).attr('data-value') == 1 ? 0 : 1;
		$.post('/admin/evaluate/set_status/'+id, {status: status}, function () {
			Toast.success('操作成功', function () {
				window.location.reload();	
			});
		});
	});

	$('.del-btn').on('click', function () {
		var id = $(this).parents('td').attr('data-id');
		$.post('/admin/evaluate/delete/'+id, function () {
			Toast.success('操作成功', function () {
				window.location.reload();
			});
		});
	});

	$('.show-detail').on('click', function () {
		var id = $(this).parents('td').attr('data-id');
		$.get('/admin/evaluate/detail/'+id, function (json) {
			$('.modal .modal-body').html('');
			$('.modal .modal-body').html(json.content);
			if (json.images) {
				var images = $('<div class="images"></div>');
				json.images.split(',').map(function (item) {
					images.append('<img src="'+item+'" />');
				});
				$('.modal .modal-body').append(images);
			}

			$('.modal').modal('show');
		});
	})
})
</script>